<template>
  <div id="app">
    <div>
      <el-button type="primary" @click="showAddDialog">添加学生</el-button>
      <el-dialog :title="form.id?'编辑学生':'添加学生'" :visible.sync="dialogVisible" width="30%">
        <el-form ref="form" :model="form" label-width="80px" v-loading="formloading">
          <el-form-item label="姓名" :rules="[{ required: true, message: '姓名不能为空'}]">
            <el-input v-model="form.name"></el-input>
          </el-form-item>
          <el-form-item label="性别" :rules="[{ required: true, message: '性别不能为空'}]">
            <el-radio-group v-model="form.gender">
              <el-radio :label="1">男</el-radio>
              <el-radio :label="0">女</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="年龄" prop="age" :rules="rules">
            <el-input v-model.number="form.age"></el-input>
          </el-form-item>
          <el-form-item label="电话" :rules="[{ required: true, message: '电话不能为空'}]">
            <el-input v-model="form.tel"></el-input>
          </el-form-item>
          <el-form-item label="详细地址" :rules="[{ required: true, message: '详细地址不能为空'}]">
            <el-input type="textarea" v-model="form.address"></el-input>
          </el-form-item>
        </el-form>

        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveuser">确 定</el-button>
        </span>
      </el-dialog>
    </div>
    <el-table :data="list" style="width: 1000px;margin:100px auto;" v-loading="loading">
      <el-table-column prop="id" label="编号" width="50"></el-table-column>
      <el-table-column prop="name" label="姓名" width="180"></el-table-column>
      <el-table-column prop="gender" label="性别" :formatter="fmtGender"></el-table-column>
      <el-table-column prop="age" label="年龄"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="tel" label="电话"></el-table-column>
      <el-table-column prop="tel" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="edituser(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="deluser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div style="display:flex;justify-content:center">
      <el-pagination
        layout="prev, pager, next"
        @current-change="getuser"
        :page-size="pageSize"
        :total="totalcount"
      ></el-pagination>
    </div>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    var checkAge = (rule, value, callback) => {
      if (!Number.isInteger(value)) {
        callback(new Error("年龄值不合法！"));
      } else {
        if (parseInt(value) < 0 || parseInt(value) > 110) {
          callback(new Error("年龄值不合法！"));
        }
      }
    };

    return {
      rules: [
        { required: true, message: "年龄不能为空" },
        { type: "number", message: "年龄必须为数字值" },
        { validator: checkAge, trigger: "blur" }
      ],
      formloading: false,
      form: {
        id: "",
        name: "",
        age: "",
        gender: 1,
        address: "",
        tel: ""
      },
      list: [],
      dialogVisible: false,
      pageSize: 3,
      loading: false,
      totalcount: 0
    };
  },
  created() {
    this.getuser();
  },
  methods: {
    showAddDialog() {
      this.form = { gender: 1 };
      this.dialogVisible = true;
    },
    onSubmit() {
      console.log("submit!");
    },
    deluser(index, row) {
      this.$confirm("确定删除?", "系统提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.$http
          .post("/remove", {
            id: row.id
          })
          .then(res => {
            if (res.data.success) {
              this.$success("删除成功");
            } else {
              this.$fail("删除失败");
            }
            this.getuser();
          });
      });
    },
    saveuser() {
      this.$http.post("saveuser", this.form).then(res => {
        if (res.data.success) {
          this.$success("保存成功");
        } else {
          this.$fail("保存失败");
        }
        this.getuser();
      });

      this.dialogVisible = false;
    },
    fmtGender({ gender }) {
      return gender ? "男" : "女";
    },
    edituser(index, row) {
      this.form = { gender: 1 };
      this.formloading = true;
      this.$http
        .get("/userinfo", {
          params: {
            id: row.id
          }
        })
        .then(res => {
          this.formloading = false;
          this.form = res.data;
        });

      this.dialogVisible = true;
    },
    getuser(pageIndex = 1) {
      this.loading = true;

      this.$http.get("/usercount").then(res => {
        this.totalcount = res.data.count;
      });

      this.$http
        .get("/user", {
          params: {
            pageIndex,
            pageSize: this.pageSize
          }
        })
        .then(res => {
          this.loading = false;
          this.list = res.data;
        });
    }
  }
};
</script>

<style>
#app {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
